<?php
/*
** 创意广告提交模板
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;
cfc('core')->checklogin();
$_RQ['op']=$_RQ['op']?$_RQ['op']:'home';
$return=array();
$return['pluginset']['ailogo']=tomedia($return['pluginset']['ailogo']);
$return['basic']=$return['pluginset'];
if($_RQ['op']=='home'){
	$mb=pdo_get('jy_aixiaoyi_mb',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']),array('id','title','fileds','notice'));
	//查询方案标题
	$title = $mb['title'];
	$return['title'] = $title;
	$mb['fileds']=$mb['fileds']?json_decode($mb['fileds'],true):array();
	foreach($mb['fileds'] as &$sv){
		if($sv['value']){
			$sv['value']=explode(';',$sv['value']);
		}
	}
	$result = '';
	$member = pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_SESSION['uid']),array('nickname','mobile'));	
	$post = array(
		'mb_id'=>intval($_RQ['id']),
		'mb_result'=>$result,
		'fileds'=>$mb['fileds']
	);
	$return['post']=$post;
	$return['banner']=$mb['hdps']?json_decode($mb['hdps'],true):$return['banner'];
	$return['uniacid'] = $_SESSION['uniacid'];
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if($postdata['fileds']){
		foreach ($postdata['fileds'] as $item) {
			if($item['ismust']==1 && empty($item['data'])){
				exi('请完善'.$item['title'].'后再提交','error');
			}
		}
	}
	$newarr = array();
	foreach($postdata['fileds'] as &$sv){

	    if($sv['type']==5){
	        $checktype = implode('',$sv['value']);
	    }
	    
		if($sv['value']){
			$sv['value']=explode(';',$sv['value']);
		}
	}
	$arr1=array();
	foreach($postdata['fileds'] as &$sv){
		if($sv['data']){
		    
			$arr1[] = $sv['data'];

		}
	}
	//如果是复选框需要进行处理
	$postdata['mb_result'] = implode('',$arr1);
	//exi('111111','error');
	//如果是复选框需要进行处理
	$insert=array(
		'uniacid'=>$_SESSION['uniacid'],
		'user_id'=>$_SESSION['uid'],
		'ms_result'=>$postdata['mb_result'],
		'tid'=>$postdata['mb_id'],
		'createtime'=>TIMESTAMP,
	);
	$chatmsg = trim($insert['ms_result']);

	$logid=$this->model('kefu')->addchatlog($chatmsg);
	$res['log']=array(
		'id'=>$logid,
		'mid'=>$_SESSION['uid'],
		'chatid'=>$_SESSION['chatid'],
		'askmsg'=>$chatmsg,
	);
	if($res['log'] && $res['log']['id']){
		if($return['basic']['pluginset']['iswenda']==1){
			exi('还有一个问题正在思考中，请稍后提问！','error');
		}
	}
	$params['key'] =  0;
	$params['logid'] =  $logid;
	$params['chatmsg'] =  $chatmsg;
	//exi('提交成功获取中...','success','jy_aixiaoyi/index');
	exi($params);
}


if($_RQ['op']=='minipost'){
	$postdata=get_postdata();
	
	$newarr = array();
	foreach($postdata['fileds'] as &$sv){

	    if($sv['type']==5){
	        $checktype = implode('',$sv['value']);
	    }
	    
		if($sv['value']){
			$sv['value']=explode(';',$sv['value']);
		}
	}
	$arr1=array();
	foreach($postdata['fileds'] as &$sv){
		if($sv['data']){
		    
			$arr1[] = $sv['title'].$sv['data'];

		}
	}
	//如果是复选框需要进行处理
	$postdata['mb_result'] = implode('',$arr1);
	
	//exi('111111','error');
	//如果是复选框需要进行处理
	$insert=array(
		'uniacid'=>$_SESSION['uniacid'],
		'user_id'=>$_SESSION['uid'],
		'ms_result'=>$postdata['mb_result'],
		'tid'=>intval($postdata['mb_id']),
		'createtime'=>TIMESTAMP,
	);
	$mb = pdo_get('jy_aixiaoyi_mb',array('uniacid'=>$_SESSION['uniacid'],'id'=>$insert['tid']));
	$chatmsg = trim('写'.$insert['ms_result'].$mb['jwcontent']);
	$logid=$this->model('kefu')->addchatlog($chatmsg);
	$res['log']=array(
		'id'=>$logid,
		'mid'=>$_SESSION['uid'],
		'chatid'=>$_SESSION['chatid'],
		'askmsg'=>$chatmsg,
	);
	if($res['log'] && $res['log']['id']){
		if($return['basic']['pluginset']['iswenda']==1){
			exi('还有一个问题正在思考中，请稍后提问！','error');
		}
	}
	$params['key'] =  0;
	$params['logid'] =  $logid;
	$params['chatmsg'] =  $chatmsg;
	//exi('提交成功获取中...','success','jy_aixiaoyi/index');
	exi($params);
}
if($_RQ['op']=='getmsg'){
	$logid = trim($_RQ['logid']);
	$chatmsg = trim($_RQ['msg']);
	$res['key'] = intval($_RQ['key']);
	$res['msg']=$this->model('member')->bi_up();
	if(!$res['msg']){
		$res['msg']=$this->model('model')->getmsg($chatmsg);
	}
	if($res['msg'] && $res['msg']!='fail'){
		pdo_update('jy_aixiaoyi_msglog',array('answer'=>$res['msg']),array('id'=>$logid,'uniacid'=>$_SESSION['uniacid']));
		$checkvip=$this->model('member')->checkvip('',true);
		if($checkvip!='isvip'){
			$basic=$this->plugin_setting('basic');
			$isgetfee=pdo_get('core_credit_log',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'plugin'=>'jy_aixiaoyi','tag'=>'bi_post'.$logid),'id');
			if(!$isgetfee){
				model('core/member')->member_credit($_SESSION['uid'],'credit3',$basic['bi_post'],false,'jy_aixiaoyi','提问消耗','bi_post'.$logid);
				model('core/member')->member_credit($_SESSION['uid'],'credit5',$basic['bi_jiankan'],true,'jy_aixiaoyi','提问获取健康币','bi_jiankan'.$logid);
			}
		}
	}
	if($res['msg']=='fail'){
		$res['fail']=1;
		$res['msg']='';
	}
	exi($res);
}